package com.winning.pmph.ctrl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.winning.pmph.entity.Authority;
import com.winning.pmph.entity.Resource;
import com.winning.pmph.entity.Role;
import com.winning.pmph.entity.User;
import com.winning.pmph.mapper.ResourceMapper;
import com.winning.pmph.service.AuthorityService;
import com.winning.pmph.service.RoleService;
import com.winning.pmph.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(value = "系统管理/角色管理")
@RestController
@RequestMapping("/pmph/action/RoleCtrl")
public class RoleCtrl {
    @javax.annotation.Resource
    private RoleService roleService;
    @javax.annotation.Resource
    private ResourceMapper resourceMapper;
    @javax.annotation.Resource
    private AuthorityService authorityService;
    @javax.annotation.Resource
    private UserService userService;

    @ApiOperation(value = "selectResource", notes = "获取菜单列表")
    @GetMapping("selectResource")
    public List<Resource> selectResource() {
        List<Resource> allLevel = resourceMapper.selectList(Wrappers.lambdaQuery(Resource.class).orderByAsc(Resource::getSortNum));
        Map<String, Resource> item1 = allLevel.stream().collect(Collectors.toMap(Resource::getId, item -> item));

        for (Resource resource : allLevel) {
            if (StringUtils.isBlank(resource.getParentId())) {
                continue;
            } else {
                Resource parentNode = item1.get(resource.getParentId());
                parentNode.getChildren().add(resource);
            }
        }
        List<Resource> oneLevelTag = allLevel.stream().filter(t -> StringUtils.isBlank(t.getParentId())).collect(Collectors.toList());
        return oneLevelTag;
    }

    @ApiOperation(value = "saveBean", notes = "编辑保存接口")
    @PostMapping("saveBean")
    public void saveBean(@RequestBody Role role) {
        if (StringUtils.equals(role.getStatus(), "false")) {
            List<User> list = userService.lambdaQuery().like(User::getRoleIds, role.getId()).list();
            list.forEach(t -> {
                t.getRoleIds().remove(role.getId());
            });
            list.stream().filter(t -> t.getRoleIds().size() == 0).forEach(user -> {
                user.setStatus(User.BLOCK);
            });
            userService.updateBatchById(list);
        }
        roleService.saveOrUpdate(role);
    }

    @ApiOperation(value = "selectTableData", notes = "查询角色列表接口")
    @GetMapping("selectTableData")
    public List<Role> selectTableData(String roleName) {
        List<Role> roleList = roleService.getRoleList(roleName);
        return roleList;
    }

    @ApiOperation(value = "delete", notes = "角色列表删除接口")
    @PostMapping("delete")
    public void delete(String id) {
        roleService.removeById(id);

    }

    @ApiOperation(value = "save", notes = "权限编辑保存接口")
    @PostMapping("save")
    public void save(String selection,String roleId) {
        List<Authority> authorities = JSONObject.parseArray(selection, Authority.class);
        authorities.forEach(t -> {
                    t.setRoleId(roleId);
                    t.setResourceId(t.getId());
                    t.setName(t.getName());
                    t.setId(null);
                }
        );

        authorityService.remove(Wrappers.lambdaQuery(Authority.class).eq(Authority::getRoleId, roleId));

        authorityService.saveOrUpdateBatch(authorities);
    }

    @ApiOperation(value = "selectAuthorityTableData", notes = "获取权限列表")
    @GetMapping("selectAuthorityTableData")
    public  List<Authority> selectAuthorityTableData(String roleId) {
        List<Authority> list = authorityService.lambdaQuery().eq(Authority::getRoleId, roleId).list();
        list.forEach(t -> {
                    t.setName(t.getName());
                    t.setId(t.getResourceId());
                }
        );
        return list;
    }

}
